<?php
/**
* FILE_NAME : index.php   FILE_PATH : id5:/id5/new/web_root/index.php
* ….接口首页
*
* @copyright Copyright www.phpunion.com
* @author slash upsetcn@gmail.com
* @package
* @subpackage
* @version Sat Dec 10 20:07:41 CST 2011
*/ 
include_once("ThePage.class.php");
$page = new ThePage();
$temp_id = $page->userRequest->get("temp_id");

if(empty($temp_id))
{
	$code = isset($_GET['code']) ? trim($_GET['code']): 0 ;
	//动态码验证
	if(!check_code($code))
	{
		user_log(0,"-2003","error",$_SERVER['SCRIPT_NAME']);
		header("HTTP/1.0 404 Not Found");
		exit();
	}
	//用户数据第一次提交
	//验证用户数据
	$user_id = isset($_POST['user_id']) ? trim($_POST['user_id']) : '';
	if(empty($user_id))
	{
		user_log(0,"-3003","error",$_SERVER['SCRIPT_NAME']);
		//用户id为空
		alertMsg('-3003');
		exit();

	}
	//合作伙伴用户名
	$user_name = isset($_POST['user_name']) ? trim($_POST['user_name']) : '';
	if(empty($user_name))
	{
		user_log(0,"-3004","error",$_SERVER['SCRIPT_NAME']);
		alertMsg('-3004');
		exit();
	}
	$icpcode = isset($_POST['icpcode']) ? trim($_POST['icpcode']) : '';
	$icp_arr = get_icpcode_info($icpcode);
	//设置使用的模板
	$page->set_tpl($icp_arr['tpl']);

	if(empty($icp_arr))
	{
		//icpcode 为空
		user_log(0,"-3005","error",$_SERVER['SCRIPT_NAME']);
		alertMsg('-3005');
		exit();
	}
	$check_code = isset($_POST['check_code']) ? trim($_POST['check_code']) : '';
	if(empty($check_code))
	{
		//检测合成码
		user_log(0,"-3006","error",$_SERVER['SCRIPT_NAME'],$icpcode,$user_id);
		alertMsg('-3006');
		exit();
	}

	$app_key = get_app_key($icp_arr['key'],$user_id,$user_name,$icpcode);
	if($check_code !== $app_key)
	{
		//检测合成码
		user_log(0,"-3007","error",$_SERVER['SCRIPT_NAME'],$icpcode,$user_id);
		alertMsg('-3007');
		exit();
	}
	$email = isset($_POST['email']) ? trim($_POST['email']) : '';

	if(!preg_match('/^[A-Za-z0-9\._-]{1,}@[A-Za-z0-9_-]{1,}\.[A-Za-z0-9_-]{2,}/',$email))
	{
		//检测邮箱
		user_log(0,"-3008","error",$_SERVER['SCRIPT_NAME'],$icpcode,$user_id);
		alertMsg('-3008');
		exit();
	}
	
	/*
	$url = isset($_POST['call_back_url']) ? trim($_POST['call_back_url']) : '';
	//preg_match('/[[:alnum:]\x80-\xff]{1,}@[[:alnum:]\x80-\xff]{1,}\.[[:alnum:]]{2,}/',$email)
	if(empty($url))
	{
		//回调函数
		user_log(0,"-3012","error",$_SERVER['SCRIPT_NAME']);
		alertMsg('-3012');
		exit();
	}
	*/
	//下面可为空
	$company = isset($_POST['company']) ? trim($_POST['company']) : '';
	$position = isset($_POST['position']) ? trim($_POST['position']) : '';
	//取得唯一id
	$temp_id = $page->get_uuid();

	//取得合作伙伴信息
	$icp_arr = get_icpcode_info($user_temp['icpcode']);
	$page->set_tpl($icpcode);

	//存数数据到mc
	$user_temp = array();
	$user_temp['uu_id'] = $temp_id;
	$user_temp['user_id'] = $user_id;
	$user_temp['user_name'] = $user_name;
	$user_temp['icpcode'] = $icpcode;
	$user_temp['email'] = $email;
	$user_temp['company'] = $company;
	$user_temp['position'] = $position;
	$user_temp['time'] = time();
	$user_temp['call_back_url'] = $icp_arr['call_back_url'];

	//将返回地址放入COOKIE
	$page->userRequest->user_set_cookie('err_url',$_SERVER['HTTP_REFERER'],3600*24);
	//状态用做是否验证完毕同步使用
	$user_temp['state']=0;
	//$rs = $page->mc->set('',time());
	//echo SFT.$temp_id;
	if($page->mc->set(SFT.$temp_id,serialize($user_temp),MEMD_OVER_TIME))
	{
		//释放唯一值到session
		$page->userRequest->set("temp_id",$temp_id);
	}else{
		//mc存储失败
		user_log($temp_id,"-3009","error",$_SERVER['SCRIPT_NAME'],$icpcode,$user_id);
		alertMsg('-3009');
		exit();
	}
}else{
	//根据temp_id 获取mc里面的数据
	$user_temp = array();
	$temp = $page->mc->get(SFT.$temp_id);
	$user_temp = @unserialize($temp);
	if(empty($user_temp))
	{
		//如果mc中数据是不存在
		user_log($temp_id,"-3010","error",$_SERVER['SCRIPT_NAME']);
		alertMsg('-3010');
		exit();
	}
	$icp_arr = get_icpcode_info($user_temp['icpcode']);
	$icpcode = $user_temp['icpcode'];
	$user_id = $user_temp['user_id'];
	//设置使用的模板
	$page->set_tpl($user_temp['icpcode']);
	if(empty($icp_arr))
	{
		user_log($temp_id,"-3011","error",$_SERVER['SCRIPT_NAME'],$icpcode,$user_id);
		alertMsg('-3011');
		exit();
	}
}
//成功结果写入日志文档
user_log($temp_id,"suc 1","suc",$_SERVER['SCRIPT_NAME'],$icpcode,$user_id);
//将信息释放给页面
$page->set_sub_info($user_temp['icpcode']);
$icp_arr = get_icpcode_info($icpcode);
$page->smarty->assign("protocol_url",$icp_arr['protocol_url']);
/*$page->smarty->assign('coop_name',$icp_arr['name']);
$page->smarty->assign('icpcode',$icp_arr['icpcode']);
$page->smarty->assign('temp_id',$temp_id);
$page->smarty->assign('user_id',$user_id);*/
//print_r($page->mc->get($temp_id));
$page->smarty->display("index.tpl");
?>
